/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Day09_SlideExamples;

/**
 *
 * @author Mark
 */
public class countOccurrances_Stack
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
       /* Stack<String> myStack = new Stack<String>();
        myStack.push("Hello");
        myStack.push("Bye");

        Iterator<String> iterator = myStack.iterator();
        while (iterator.hasNext())
        {
            System.out.println(iterator.next());
        }

        System.out.println("*******************");

        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
		*/

        String[] wordList = {"Mark", "James", "Mark", "Paul", "Peter", "Mark", null};
        String word = "Mark";

        int ct = countOccurrences( wordList, "Mark" );
        System.out.println("String count is: " + ct);
        
        Integer[] myInt = {1,2,3,4,5,5,56,5};
        System.out.println("Integer Count is:"+ countOccurrences(myInt, 5));

    }
    
    
    public static <T> int countOccurrences(T[] list, T itemToCount)
    {
        int count = 0;
        if (itemToCount == null)
        {
            for (T listItem : list)
            {
                if (listItem == null)
                {
                    count++;
                }
            }
        } else
        {
            for (T listItem : list)
            {
                if (itemToCount.equals(listItem))
                {
                    count++;
                }
            }
        }
        return count;
    }
}
